home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c,gnu.gcc.help
- Subject: Re: Casting unsigned short as unsigned int -> Bus error
- Date: 5 Jan 1996 21:19:26 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4ck4ku$i5s@news.iag.net>
- References: <simmons.820857453@rzdspc1>
- NNTP-Posting-Host: pm3-orl15.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <simmons.820857453@rzdspc1>,
- simmons@rzdspc1.informatik.uni-hamburg.de says...
- >
- >I have a pointer to a struct with a member declared as unsigned short:
- >
- > typedef struct {
- > ...
- > unsigned short myshort;
- > ...
- > } RecType;
- >
- > RecType *MyRec;
- >
- >Now when I try to dereference that member, casting it to (unsigned int),
- >I get a bus error:
- >
- > unsigned int myint;
- >
- > myint = (unsigned int) MyRec->myshort; /* Bus error! */
- >
- >I'm using GCC 2.7.2 on a SparcStation running SunOS 4.1.4. I've made sure
- >that the pointer points to valid data.
-
- I wish you had posted the code you used for this, because it certainly looks
- like you are trying to dereference an invalid or NULL pointer.
-
- Try this snippet. See if it gives you any problem.
-
-
- #include <stdio.h>
- #include <stdlib.h>
-
- typedef struct
- {
- unsigned short myshort;
- } RecType;
-
- int main( void)
- {
- RecType MyRec, *MyRecPtr;
- unsigned int myint;
-
- MyRecPtr = malloc( sizeof(RecType));
- if( MyRecPtr == NULL)
- {
- fprintf( stderr, "You've got a real memory problem here.\n");
- exit(EXIT_FAILURE);
- }
-
- MyRec.myshort = 4;
- MyRecPtr->myshort = 6;
- myint = MyRec.myshort + MyRecPtr->myshort;
-
- printf( "%u %u %u\n", (unsigned int)MyRec.myshort,
- (unsigned int)MyRecPtr->myshort,
- myint);
- return 0;
- }
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-